package com.efesco.finance.oughtReceive;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.efesco.common.DBTools;
import com.efesco.common.Tools;
import com.efesco.system.User;

/**
 * queryOughtReceive_oughtReceiveDatasetListener
 */
public class queryOughtReceive_oughtReceiveDatasetListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
    String fesc_user_id="",sql="",foreign_enterprise_id="";
  	
  	DoradoContext context = DoradoContext.getContext();
    Object obj = context.getAttribute(context.SESSION, "User");
    if (obj != null) {
    	User user = (User) obj;
    	int roleType = user.getRoleType();
    	if (roleType == 2)  {
    		fesc_user_id=user.getForEmployeeId();
    		foreign_enterprise_id=user.getForeignEnterpriseId();
    		sql = "SELECT cmp.COMPANY_CHINESE_NAME,o.ought_month,pv.PAYMENT_ID,pv.VERSION_ID,o.ought_amount," +
					"o.PAYMENT_VERSION_ID paymentVersionId,o.payment_type,o.PAYMENT_VERSION_ID,to_char(o.closed_date,'yyyy-mm-dd') closed_date " +
					"FROM " +  
					"s_payment_version pv,s_single_company cmp,s_ought_receive o," +
					"s_company_businessman cbm " + 
					"WHERE "+ 
					"o.PAYMENT_VERSION_ID = pv.PAYMENT_VERSION_ID AND " + 
					"pv.SINGLE_COMPANY_ID = cmp.SINGLE_COMPANY_ID AND " +
					"cmp.single_company_id=cbm.single_company_id and " + 
					" cmp.FOREIGN_ENTERPRISE_ID = " + foreign_enterprise_id  +
					" and exists " + DBTools.getRightSqlByBusinessman(fesc_user_id,"cbm") ;
    	}
    	else  {
    		sql = "SELECT cmp.COMPANY_CHINESE_NAME,o.ought_month,pv.PAYMENT_ID,pv.VERSION_ID,o.ought_amount," +
				"o.PAYMENT_VERSION_ID paymentVersionId,o.payment_type,o.PAYMENT_VERSION_ID,to_char(o.closed_date,'yyyy-mm-dd') closed_date " +
				"FROM " +  
				"s_payment_version pv,s_single_company cmp,s_ought_receive o " +
				"WHERE "+ 
				"o.PAYMENT_VERSION_ID = pv.PAYMENT_VERSION_ID AND " + 
				"pv.SINGLE_COMPANY_ID = cmp.SINGLE_COMPANY_ID ";
    	}
    	if (!Tools.toStr(dataset.parameters().getString("singleCompanyId")).equals("")
    		&& !Tools.toStr(dataset.parameters().getString("singleCompanyId")).equals("null")	)  {
    		sql = sql + " and cmp.single_company_id = " + dataset.parameters().getString("singleCompanyId");
    	}
    	if (!Tools.toStr(dataset.parameters().getString("oughtMonth1")).equals("")
        	&& !Tools.toStr(dataset.parameters().getString("oughtMonth1")).equals("null")	)  {
        	sql = sql + " and o.ought_month >= '" + dataset.parameters().getString("oughtMonth1")+"'";
        }
    	if (!Tools.toStr(dataset.parameters().getString("oughtMonth2")).equals("")
            && !Tools.toStr(dataset.parameters().getString("oughtMonth2")).equals("null")	)  {
            sql = sql + " and o.ought_month <= '" + dataset.parameters().getString("oughtMonth2")+"'";
        }
    	if (!Tools.toStr(dataset.parameters().getString("amount1")).equals("")
    		&& !Tools.toStr(dataset.parameters().getString("amount1")).equals("null")	)  {
            sql = sql + " and o.ought_amount >= " + dataset.parameters().getString("amount1");
        }
    	if (!Tools.toStr(dataset.parameters().getString("amount2")).equals("")
        	&& !Tools.toStr(dataset.parameters().getString("amount2")).equals("null")	)  {
            sql = sql + " and o.ought_amount <= " + dataset.parameters().getString("amount2");
        }
    	if (!Tools.toStr(dataset.parameters().getString("businessmanId")).equals("")
        	&& !Tools.toStr(dataset.parameters().getString("businessmanId")).equals("null")	)  {
        	sql = sql + " and cbm.businessman_id = " + dataset.parameters().getString("businessmanId");
        }    	
    	if (!Tools.toStr(dataset.parameters().getString("paymentType")).equals(""))  {
        	sql = sql + " and o.payment_type = " + dataset.parameters().getString("paymentType");
        }
    	sql = sql + " order by o.ought_month,cmp.COMPANY_CHINESE_NAME ";
    	SqlDataset sqlDataset=(SqlDataset)dataset;
    	sqlDataset.setSql(sql);
    }
  	
    return true;
  }

}
